package com.codemes.happylist.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author nscoffee
 */
@Slf4j
public class DocumentUtil {

    public static List<Node> getElementByTagName(String tagName, Node node){
        if(StringUtils.isEmpty(tagName)||null==node){
            return Collections.emptyList();
        }
        List<Node> nodeList = new ArrayList<>();
        getElementByTagName(tagName,node,nodeList);
        return nodeList;
    }

    public static void getElementByClass(String className,Node node,List<Node> nodeList){
        if(null==nodeList){
            nodeList = new ArrayList<>();
        }
        if(node.getNodeName().equalsIgnoreCase("#text")){
            return;
        }
        NodeList childrenNodes = node.getChildNodes();
        if(null!=childrenNodes){
            int len = childrenNodes.getLength();
            for(int i=0;i<len;i++){
                Node childrenNode = childrenNodes.item(i);
                if(null==childrenNode.getAttributes()){
                    continue;
                }
                Node classNode = childrenNode.getAttributes().getNamedItem("class");
                if(null==classNode){
                    continue;
                }
                String classInfo = classNode.getNodeValue().toLowerCase();
                if(classInfo.contains(className)){
                    nodeList.add(childrenNode);
                }
                getElementByTagName(className,childrenNode,nodeList);
            }
        }
    }

    public static String toHtml(Node node){
        try {
            DOMSource domSource = new DOMSource(node);
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            StringWriter sw = new StringWriter();
            StreamResult sr = new StreamResult(sw);
            transformer.transform(domSource, sr);
            return sw.toString();
        } catch (TransformerException e) {
            e.printStackTrace();
            log.error("transform node to html occur error -> {]", e);
        }
        return null;
    }

    private static void getElementByTagName(String tagName,Node node,List<Node> nodeList){
        if(null==nodeList){
            nodeList = new ArrayList<>();
        }
        if(node.getNodeName().equalsIgnoreCase("#text")){
            return;
        }
        NodeList childrenNodes = node.getChildNodes();
        if(null!=childrenNodes){
            int len = childrenNodes.getLength();
            for(int i=0;i<len;i++){
                Node childrenNode = childrenNodes.item(i);
                if(childrenNode.getNodeName().equalsIgnoreCase(tagName)){
                    nodeList.add(childrenNode);
                }
                getElementByTagName(tagName,childrenNode,nodeList);
            }
        }
    }
}
